<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'门店列表'),
	array('op'=>'edit','title'=>'添加门店'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','address','status','phone','province','city','district','image');
	$list=pdo_getslice('jy_bdd_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
				$item['pcd']=array('province'=>$item['province'],'city'=>$item['city'],'district'=>$item['district']);
				$item['address']=$item['pcd']['province'].' '.$item['pcd']['city'].' '.$item['pcd']['district'].' '.$item['pcd']['street'].' '.$item['address'];
			}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('tip'=>'','data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'门店名称','type'=>'string'),
			array('name'=>'image','title'=>'门店logo','type'=>'image'),
			array('name'=>'phone','title'=>'电话','type'=>'string'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			//array('type'=>'href','action'=>'store.s_item.s_count/','title'=>'管理'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$this->session('store_id',$id);
	}else{
		//set_html('framess',$this->menustore());
		//$id=$this->session('store_id');
	}
	if($id){
		$item=pdo_get('jy_bdd_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['pcd']=array('province'=>$item['province'],'city'=>$item['city'],'district'=>$item['district']);
	//	$item['worktime']=json_decode($item['worktime'],true);
		$item['lat_lng']=json_decode($item['lat_lng'],true);
		$item['bannner']=json_decode($item['bannner'],true);
		$item['store_lesson']=json_decode($item['store_lesson'],true);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回门店列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'门店名称','type'=>'string'),
					array('name'=>'image','title'=>'门店图片','type'=>'image'),
					array('name'=>'pcd','title'=>'地区','type'=>'pcd'),
					array('name'=>'lat_lng','title'=>'坐标','type'=>'lat_lng'),
					array('name'=>'address','title'=>'地址','type'=>'string'),
					array('name'=>'phone','title'=>'电话','type'=>'string'),
					array('name'=>'worktime','title'=>'营业时间','type'=>'time_hour'),
					array('type'=>'array','name'=>'bannner','add_tip'=>'添加一个幻灯片',
					'list'=>array(
						array('type'=>'image','name'=>'image','title'=>'幻灯片'),	
						array('type'=>'string','name'=>'url','title'=>'幻灯片url'),	
					),
					'details'=>array(
						array('type'=>'image','name'=>'image','title'=>'幻灯片'),	
						array('type'=>'string','name'=>'url','title'=>'幻灯片url'),
					)
					),
					array('name'=>'show','title'=>'门店介绍','type'=>'richtext'),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
			array(
				'title'=>'课程设置',
				'list'=>array(
					array('type'=>'array','name'=>'store_lesson','add_tip'=>'添加一个课程',
					'list'=>array(
					array('name'=>'xinqi','title'=>'星期','type'=>'select','options'=>'xinqi'),
					array('name'=>'lessonid','title'=>'课程','type'=>'string'),
					array('name'=>'workerid','title'=>'授课教练','type'=>'string'),
					array('name'=>'time','title'=>'上课时间','type'=>'time_hour'),
					),
					'details'=>array(
						array('name'=>'xinqi','title'=>'星期','type'=>'select','options'=>'xinqi','option_value'=>'title'),
						array('type'=>'choose','title'=>'选择课程','name'=>'lessonid','action'=>'get_lesson','choose_title'=>'选择课程'),
						array('type'=>'choose','title'=>'选择教练','name'=>'workerid','action'=>'get_worker','choose_title'=>'选择教练'),
						array('name'=>'time','title'=>'上课时间','type'=>'time_hour')
					)
					),
				)
			),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['xinqi']=array(array('id'=>'1','title'=>'周一'),array('id'=>'2','title'=>'周二'),array('id'=>'3','title'=>'周三'),array('id'=>'4','title'=>'周四'),array('id'=>'5','title'=>'周五'),array('id'=>'6','title'=>'周六'),array('id'=>'7','title'=>'周日'));
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
//	$item['pcd']=json_decode($item['pcd'],true);
	$postdata['province']=$postdata['pcd']['province'];
	$postdata['city']=$postdata['pcd']['city'];
	$postdata['district']=$postdata['district'];
	$postdata['worktime']=ijson_encode($postdata['worktime']);
	$postdata['bannner']=ijson_encode($postdata['bannner']);
	$postdata['store_lesson']=ijson_encode($postdata['store_lesson']);
	$postdata['lat_lng']=ijson_encode($postdata['lat_lng']);
	$data=array_fields(array('title','image','lat_lng','address','phone','worktime','bannner','show','paixu','status','province','city','district','store_lesson'),$postdata);
	$id=intval($postdata['id']);
	
	if($id){
		pdo_update('jy_bdd_store',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_bdd_store',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_bdd_store',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的门店！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_bdd_store',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}